package controllers

import (
	"github.com/kataras/iris/v12"
	"storeManageApi/models/requestModels"
	"storeManageApi/models/requestModels/request_manage_models"
	"storeManageApi/models/responseModels"
	"storeManageApi/service"
)

type ManageController struct {
	Ctx           iris.Context
	Response      service.IResponse
	ManageService service.ImanageService
}

// GetGoodsOptions
//
//	@Description: /api/manage/goods/options
//	@receiver: request
//	@return: responseModels.Response
func (request ManageController) GetGoodsOptions() responseModels.Response {
	options, err := request.ManageService.GetAllGoodsOptions()
	if err != nil {
		return request.Response.FORBIDDEN(err.Error())
	}
	return request.Response.OK(responseModels.PageData{List: options})
	//return request.Response.DEVELOP()
}

// PostOutNotesPage
//
//	@Description: /api/manage/out/notes/page
//	@receiver: request
//	@return: responseModels.Response
func (request ManageController) PostOutNotesPage() responseModels.Response {
	var query requestModels.QueryNoteList = requestModels.QueryNoteList{}
	request.Ctx.ReadJSON(&query)
	err := query.Isvalidated()
	if err != nil {
		return request.Response.IsvalidatedError(err.Error())
	}
	results, queryErr := request.ManageService.GetOutNotesPage(query)
	if queryErr != nil {
		return request.Response.FORBIDDEN(queryErr.Error())
	}
	var responseData responseModels.PageData = responseModels.PageData{
		Page:     query.Page,
		PageSize: query.PageSize,
		Start:    query.Start,
		End:      query.End,
		List:     results,
		Total:    request.ManageService.GetOutNotesCount(),
	}
	return request.Response.OK(responseData)
	//return request.Response.DEVELOP()
}

// PostInNotesPage
//
//	@Description: /api/manage/in/notes/page
//	@receiver: request
//	@return: responseModels.Response
func (request ManageController) PostInNotesPage() responseModels.Response {
	var query requestModels.QueryNoteList = requestModels.QueryNoteList{}
	request.Ctx.ReadJSON(&query)
	err := query.Isvalidated()
	if err != nil {
		return request.Response.IsvalidatedError(err.Error())
	}
	results, queryErr := request.ManageService.GetInNotesPage(query)
	if queryErr != nil {
		return request.Response.FORBIDDEN(queryErr.Error())
	}
	var responseData responseModels.PageData = responseModels.PageData{
		Page:     query.Page,
		PageSize: query.PageSize,
		Start:    query.Start,
		End:      query.End,
		List:     results,
		Total:    request.ManageService.GetInNotesCount(),
	}
	return request.Response.OK(responseData)
	//return request.Response.DEVELOP()
}

// PostAddInNotes
//
//	@Description: /api/manage/add/in/notes
//	@receiver: request
//	@return: responseModels.Response
func (request ManageController) PostAddInNotes() responseModels.Response {
	var form request_manage_models.NewInNotes = request_manage_models.NewInNotes{}
	request.Ctx.ReadJSON(&form)
	err := form.Isvalidated()
	if err != nil {
		return request.Response.IsvalidatedError(err.Error())
	}
	_, addErr := request.ManageService.AddInNOtes(form)
	if addErr != nil {
		return request.Response.FORBIDDEN(addErr.Error())
	}
	return request.Response.OK("入库成功")
	//return request.Response.DEVELOP()
}

// PostAddGoodsConfig
//
//	@Description: /api/manage/add/goods/config
//	@receiver: request
//	@return: responseModels.Response
func (request ManageController) PostAddGoodsConfig() responseModels.Response {
	var form request_manage_models.NewGoodsConfig = request_manage_models.NewGoodsConfig{}
	request.Ctx.ReadJSON(&form)
	err := form.Isvalidated()
	if err != nil {
		return request.Response.IsvalidatedError(err.Error())
	}
	_, addErr := request.ManageService.AddGoodsConfig(form)
	if addErr != nil {
		return request.Response.FORBIDDEN(addErr.Error())
	}
	return request.Response.OK("添加成功")
	//return request.Response.DEVELOP()
}

// PostAddOutNotes
//
//	@Description: /api/manage/add/out/notes
//	@receiver: request
//	@return: responseModels.Response
func (request ManageController) PostAddOutNotes() responseModels.Response {
	var form request_manage_models.NewOutNotes = request_manage_models.NewOutNotes{}
	request.Ctx.ReadJSON(&form)
	err := form.Isvalidated()
	if err != nil {
		return request.Response.IsvalidatedError(err.Error())
	}
	_, addErr := request.ManageService.AddOutNotes(form)
	if addErr != nil {
		return request.Response.FORBIDDEN(addErr.Error())
	}
	return request.Response.OK("出库成功")
	//return request.Response.DEVELOP()
}

// PostStoreNotesPage
//
//	@Description: /api/manage/store/notes/page
//	@receiver: request
//	@return: responseModels.Response
func (request ManageController) PostStoreNotesPage() responseModels.Response {
	var query requestModels.QueryNoteList = requestModels.QueryNoteList{}
	request.Ctx.ReadJSON(&query)
	err := query.Isvalidated()
	if err != nil {
		return request.Response.IsvalidatedError(err.Error())
	}
	results, queryErr := request.ManageService.GetStoreNotesPage(query)
	if queryErr != nil {
		return request.Response.FORBIDDEN(queryErr.Error())
	}
	var responseData responseModels.PageData = responseModels.PageData{
		Page:     query.Page,
		PageSize: query.PageSize,
		Start:    query.Start,
		End:      query.End,
		List:     results,
		Total:    request.ManageService.GetStoreCount(),
	}
	return request.Response.OK(responseData)
	return request.Response.DEVELOP()
}

// PostGoodsConfigPage
//
//	@Description: /api/manage/goods/config/page
//	@receiver: request
//	@return: responseModels.Response
func (request ManageController) PostGoodsConfigPage() responseModels.Response {
	var query requestModels.QueryNoteList = requestModels.QueryNoteList{}
	request.Ctx.ReadJSON(&query)
	err := query.Isvalidated()
	if err != nil {
		return request.Response.IsvalidatedError(err.Error())
	}
	results, queryErr := request.ManageService.GetGoodsConfigPage(query)
	if queryErr != nil {
		return request.Response.FORBIDDEN(queryErr.Error())
	}
	var responseData responseModels.PageData = responseModels.PageData{
		Page:     query.Page,
		PageSize: query.PageSize,
		Start:    query.Start,
		End:      query.End,
		List:     results,
		Total:    request.ManageService.GetGoodsConfigCount(),
	}
	return request.Response.OK(responseData)
	//return request.Response.DEVELOP()
}
